package org.arestaurant;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.Map;

import javolution.util.FastMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class FoodServices {
    public static JSONArray getFoodList() {
        JSONArray foodList = null;
        InputStream in;
        Map<String, String> parameters = FastMap.newInstance();
        try {
            if(Foods.CATEGORY_ID_SELECTED != null) {
                parameters.put("productCategoryId", Foods.CATEGORY_ID_SELECTED);
            }
            ConnectionServices.connectHttp("/arestaurant/control/getFoodList", parameters);
            in = new BufferedInputStream(ConnectionServices.HTTP_CONNECTION.getInputStream());
            BufferedReader rd = new BufferedReader(new InputStreamReader(in));
            JSONObject jsonObj;
            jsonObj = new JSONObject(rd.readLine());
            foodList = (JSONArray) jsonObj.getJSONArray("foodList");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return foodList;
    }
    public static JSONObject getFood(HttpURLConnection conn) {
        JSONObject food = null;
        InputStream in;
        try {
            in = new BufferedInputStream(conn.getInputStream());
            BufferedReader rd = new BufferedReader(new InputStreamReader(in));
            JSONObject jsonObj;
            jsonObj = new JSONObject(rd.readLine());
            food = (JSONObject) jsonObj.getJSONObject("food");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return food;
    }
    public static JSONArray getCategoryList() {
        JSONArray categoryList = null;
        InputStream in;
        try {
            ConnectionServices.connectHttp("/arestaurant/control/getCategoryList");
            in = new BufferedInputStream(ConnectionServices.HTTP_CONNECTION.getInputStream());
            BufferedReader rd = new BufferedReader(new InputStreamReader(in));
            JSONObject jsonObj;
            jsonObj = new JSONObject(rd.readLine());
            categoryList = (JSONArray) jsonObj.getJSONArray("categoryList");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return categoryList;
    }
}